package com.blog.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import com.blog.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.blog.system.mapper.RoomTimeMapper;
import com.blog.system.domain.RoomTime;
import com.blog.system.service.IRoomTimeService;
import com.blog.common.core.text.Convert;

/**
 * 自习室时间段Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-13
 */
@Service
public class RoomTimeServiceImpl extends ServiceImpl<RoomTimeMapper,RoomTime> implements IRoomTimeService

{
    @Autowired
    private RoomTimeMapper roomTimeMapper;

    /**
     * 查询自习室时间段
     * 
     * @param id 自习室时间段主键
     * @return 自习室时间段
     */
    @Override
    public RoomTime selectRoomTimeById(Long id)
    {
        return roomTimeMapper.selectRoomTimeById(id);
    }

    /**
     * 查询自习室时间段列表
     * 
     * @param roomTime 自习室时间段
     * @return 自习室时间段
     */
    @Override
    public List<RoomTime> selectRoomTimeList(RoomTime roomTime)
    {
        return roomTimeMapper.selectRoomTimeList(roomTime);
    }

    /**
     * 新增自习室时间段
     * 
     * @param roomTime 自习室时间段
     * @return 结果
     */
    @Override
    public int insertRoomTime(RoomTime roomTime)
    {
        roomTime.setCreateTime(DateUtils.getNowDate());
        return roomTimeMapper.insertRoomTime(roomTime);
    }

    /**
     * 修改自习室时间段
     * 
     * @param roomTime 自习室时间段
     * @return 结果
     */
    @Override
    public int updateRoomTime(RoomTime roomTime)
    {
        roomTime.setUpdateTime(DateUtils.getNowDate());
        return roomTimeMapper.updateRoomTime(roomTime);
    }

    /**
     * 批量删除自习室时间段
     * 
     * @param ids 需要删除的自习室时间段主键
     * @return 结果
     */
    @Override
    public int deleteRoomTimeByIds(String ids)
    {
        return roomTimeMapper.deleteRoomTimeByIds(Convert.toStrArray(ids));
    }

//    /**
//     * 批量软删除自习室时间段
//     *
//     * @param ids 需要软删除的自习室时间段主键
//     * @return 结果
//     */
//    @Override
//    public int softDeleteRoomTimeByIds(String ids)
//    {
//        String[] strArray = Convert.toStrArray(ids);
//        int count = 0;
//        for (String s : strArray) {
//            RoomTime roomTime = roomTimeMapper.selectRoomTimeById(Long.valueOf(s));
//            int i = roomTimeMapper.update(null, Wrappers.<RoomTime>lambdaUpdate()
//                    .set(RoomTime::getIsDelete, 1L)
//                    .eq(RoomTime::getId, roomTime.getId()));
//            count = count + i;
//        }
//        return count;
//    }

    /**
     * 删除自习室时间段信息
     * 
     * @param id 自习室时间段主键
     * @return 结果
     */
    @Override
    public int deleteRoomTimeById(Long id)
    {
        return roomTimeMapper.deleteRoomTimeById(id);
    }

    @Override
    public List<RoomTime> getRoomsByUserName(String userName) {
        return roomTimeMapper.getRoomsByUserName(userName);
    }

    @Override
    public boolean checkConflict(String roomId ,String seatId, LocalDateTime startTime, LocalDateTime endTime) {
        LocalDate reserveDate = startTime.toLocalDate();
        int conflictCount = roomTimeMapper.countTimeConflict(roomId,
                seatId,
                reserveDate,
                startTime,
                endTime
        );
        return conflictCount > 0;
    }

    @Override
    public String getStudyLength(String userName) {
        return roomTimeMapper.getStudyLength(userName);
    }

    @Override
    public String getAvgDaily(String userName) {
        return roomTimeMapper.getAvgDaily(userName);
    }

}
